package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.zzyl.base.BaseEntity;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ResourceMapper;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.NoProcessing;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeItemVo;
import com.zzyl.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    ResourceMapper resourceMapper;

    /**
     * 资源列表
     *
     * @param resourceDto
     * @return
     */
    @Override
    public List<ResourceVo> list(ResourceDto resourceDto) {
        if (ObjectUtil.isNotEmpty(resourceDto.getParentResourceNo())) {
            resourceDto.setParentResourceNo(NoProcessing.processString(resourceDto.getParentResourceNo()));
        }
        List<Resource> resourceList = resourceMapper.list(resourceDto);
        return BeanUtil.copyToList(resourceList, ResourceVo.class);
    }

    /**
     * 树形菜单
     *
     * @param resourceDto
     * @return
     */
    @Override
    public TreeVo treeList(ResourceDto resourceDto) {
        resourceDto.setParentResourceNo(NoProcessing.processString(SuperConstant.ROOT_PARENT_ID));
        resourceDto.setDataState(SuperConstant.MENU);

        // 查询所有资源
        List<Resource> resourceList = resourceMapper.list(resourceDto);

        // 第一层集合
        List<TreeItemVo> treeItemVoList = new ArrayList<>();

        // 生成children
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setNameKey("label");
        List<Tree<String>> treeList = TreeUtil.build(resourceList, SuperConstant.ROOT_PARENT_ID, treeNodeConfig, (resource, treeItem) -> {
            treeItem.setId(resource.getResourceNo());
            treeItem.setName(resource.getResourceName());
            treeItem.setParentId(resource.getParentResourceNo());
        });
        List<TreeItemVo> itemVos = BeanUtil.copyToList(treeList, TreeItemVo.class);

        treeItemVoList.add(TreeItemVo.builder().id(SuperConstant.ROOT_PARENT_ID).label("智慧养老院").children(itemVos).build());

        return TreeVo.builder().items(treeItemVoList).build();
    }

    /**
     * 资源添加
     *
     * @param resourceDto
     */
    @Override
    public void add(ResourceDto resourceDto) {
        Resource resource = BeanUtil.toBean(resourceDto, Resource.class);
        // 找父资源 同步状态
        Resource parentResource = resourceMapper.getResourceByParentResourceNo(resource.getParentResourceNo());
        resource.setDataState(parentResource.getDataState());

        // 获取编号
        String resourceNo = getResourceNo(resource);
        resource.setResourceNo(resourceNo);

        resourceMapper.insert(resource);
    }

    /**
     * 获取资源编号
     * @param resource
     * @return
     */
    private String getResourceNo(Resource resource) {
        // 判断当前父资源编号是否为三级菜单 100001001001000 = 三级菜单: 100001001001 会去除连续3个0 长度15除以3所以等于4  如果只是二级菜单为:100001001 长度9除以3不等于4
        if (ObjectUtil.equalsAny(resource.getResourceType(), SuperConstant.MENU) &&
                NoProcessing.processString(resource.getParentResourceNo()).length() / 3 == 4) {
            // 最多只支持三级菜单
            throw new BaseException(BasicEnum.RESOURCE_DEPTH_UPPER_LIMIT);
        }

        // 查找父资源是否有其他子资源
        List<Resource> resourceList = resourceMapper.getChildrenByParentResourceNo(resource.getParentResourceNo());

        // 获取资源编号
        if (ObjectUtil.isNotEmpty(resourceList)) {
            // 获取最大资源编号
            Long maxNo = resourceList.stream().map(resource1 -> {
                return Long.valueOf(resource1.getResourceNo());
            }).max(Comparator.comparing(Long::intValue)).get();
            // 有子资源
            return NoProcessing.createNo(String.valueOf(maxNo),true);
        } else {
            // 没有子资源
            return NoProcessing.createNo(resource.getParentResourceNo(),false);
        }
    }
}
